package com.niiwoo.civet.user.service.duboo.asset;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.enums.TradeConfigEnum;
import com.niiwoo.civet.trade.service.configure.TradeConfigDubboService;
import com.niiwoo.civet.user.dao.entity.UserCreditInfo;
import com.niiwoo.civet.user.dto.request.asset.AssetCreditResultDTO;
import com.niiwoo.civet.user.dto.response.cardniu.CardNiuCreditInfoDTO;
import com.niiwoo.civet.user.enums.CreditEnum;
import com.niiwoo.civet.user.service.asset.AssetCreditDubboService;
import com.niiwoo.civet.user.service.local.UserCreditService;
import com.niiwoo.civet.user.service.local.asset.AssetCreditService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author LiJian
 * @date 2018/2/22
 */
@Slf4j
@Service(version = "1.0.0")
public class AssetCreditDubboServiceImpl implements AssetCreditDubboService {

    @Autowired
    private UserCreditService creditLocalService;

    @Autowired
    private AssetCreditService assetCreditService;

    @Reference(version = "1.0.0")
    private TradeConfigDubboService tradeConfigDubboService;

    @Override
    public void finishAssetCredit(AssetCreditResultDTO assetCreditResultDTO) {
        log.info("卡牛完成授信更新授信信息, assetCreditResultDTO={}", JSON.toJSONString(assetCreditResultDTO));
        assetCreditService.updateCreditResult(assetCreditResultDTO);

    }

    /**
     * 获取授信信息
     * @param userId
     * @return
     */
    @Override
    public CardNiuCreditInfoDTO getUserCardNiuCreditInfo(String userId) {

        CardNiuCreditInfoDTO cardNiuCreditInfoDTO = new CardNiuCreditInfoDTO();
        cardNiuCreditInfoDTO.setCreditStatus(0);

        UserCreditInfo userCreditInfo = creditLocalService.getUserCreditInfoByType(userId, CreditEnum.CreditAmountType.CARD_NIU.getType());
        if (userCreditInfo != null) {
            cardNiuCreditInfoDTO.setCreditTotalAmount(userCreditInfo.getCreditAmount());
            cardNiuCreditInfoDTO.setCreditModelType(userCreditInfo.getCreditModelType());
            if (CreditEnum.NeedUpdateStatus.YES.getType() == userCreditInfo.getNeedUpdate().byteValue()) {
                cardNiuCreditInfoDTO.setCreditStatus(3);
            } else {
                cardNiuCreditInfoDTO.setCreditStatus(this.isCreditExpired(userCreditInfo.getCreditTime()) ? 3 : 2);
            }
            cardNiuCreditInfoDTO.setCreditTime(userCreditInfo.getCreditTime());
        }
        return cardNiuCreditInfoDTO;
    }


    /**
     * 判断授信是否过期
     *
     * @param creditDate
     * @return
     */
    private boolean isCreditExpired(Date creditDate) {
        DateTime creditTime = new DateTime(creditDate).withTimeAtStartOfDay();
        // 获取授信过期天数
        List<TradeConfigEnum> enumList = new ArrayList<>();
        enumList.add(TradeConfigEnum.CREDITOVERDUE);
        Map<String, String> creditOverdueConfig = tradeConfigDubboService.selectTradeConfigByEmuns(enumList);
        String expireDaysStr = creditOverdueConfig.get(TradeConfigEnum.CREDITOVERDUE.getConfigKey());
        if(StringUtils.isBlank(expireDaysStr)) {
            return false;
        }
        int expireDays = Integer.parseInt(expireDaysStr);
        if (creditTime.plusDays(expireDays).isBeforeNow()) {
            return true;
        }
        return false;
    }
}
